package com.ccys.cms.controller.productAndService;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.productAndService.ProductListDetailSerivce;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.web.productAndService.BatchDetailProductDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.dto.web.productAndService.ProductListDetailDto;
import com.ccys.db.entity.TblProductListDetail;
import com.ccys.db.mapper.TblProductListDetailMapper;
import com.ccys.db.vo.web.productAndService.ExistProductVo;
import com.ccys.db.vo.web.productAndService.ProductListDetailVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/productAndService")
@Api(tags = "产品集合详情管理")
public class ProductListDetailContoller {


    @Resource
    private TblProductListDetailMapper mapper;

    @Resource
    private ProductListDetailSerivce service;

    @ApiOperation("批量添加Product")
    @PostMapping("productListDetail")
    @Transactional
    public ResponseResult saveBatch(@RequestBody List<BatchDetailProductDto> dtoList) {

        if (CollectionUtils.isNotEmpty(dtoList)) {
            BatchDetailProductDto dto = dtoList.get(0);
            String listId = dto.getListId();
            //查询药品是否已经存在
            List<ExistProductVo> existProductList = mapper.getExistProductList(listId, dtoList);
            if (CollectionUtils.isNotEmpty(existProductList)) {
               return ResponseResult.error("Product " + existProductList.get(0).getName() + " already exists");
            }
            List<TblProductListDetail> collect = dtoList.stream().map(x -> {
                TblProductListDetail detail = new TblProductListDetail();
                detail.setListId(listId);
                detail.setProductId(x.getProductId());
                detail.setId(IdUtil.getSnowflakeNextIdStr());
                detail.setCreatedOnUtc(new Date().getTime());
                detail.setCreatedBy(UserUtil.getCurLoginUserId());
                return detail;
            }).collect(Collectors.toList());

            int i = service.insertAll(collect);

            return ResponseResult.update(i);
        }

        return ResponseResult.success();

    }


    @ApiOperation("编辑coverd diagnoses")
    @PutMapping("productListDetail")
    @Transactional
    public ResponseResult update(TblProductListDetail model) {

        int i = service.updateEntityById(model);
        return ResponseResult.update(i);
    }

    @DeleteMapping("productListDetail")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblProductListDetail model = new TblProductListDetail();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        return ResponseResult.update(i);
    }


    @GetMapping("productListDetail")
    @ApiOperation(value = "查询分页")
    public ResponseResult<PageInfo<ProductListDetailVo>, Object> getPage(ProductListDetailDto dto) {

        return service.getPage(dto);
    }
}
